

function RndNum(n)
{
    var rnd="";
    for(var i=0;i<n;i++)
        rnd+=Math.floor(Math.random()*10);
    return rnd;
}
//生成六位随机数
function randomNo(id){
	$('#'+id).val(RndNum(6));
}

/**
 * 注册协议
 */
function readUserAgreement(){
	var winHeight = $(window).height(); 
	$.dialog({
	    title: '<fmt:message key="i18n.lookAgreement"/>',lock: true,
	    width: 620, height: (winHeight * 0.6),//仅能为数字
	    content: $('#userAgreement').html(),
	    okValue: '<fmt:message key="i18n.consentAgreement"/>', ok: function () {
	    	$("#register_form").submit();
	    },
		cancelValue: ' <fmt:message key="i18n.cancel"/> ', cancel: function () {
	    }
	});
}


/**
 * 注册表单验证
 */
function checkRegisterForm(){
	
	if($('#registerAccount').val()==''){
		alert('登录账号不能为空!');
		$('#registerAccount').focus();
		return false;
	}
	var re =new RegExp("^[A-Za-z0-9]{6,10}$");
	if (!re.test($('#registerAccount').val())){
		//alert("登录账号最少6位，只能由英文和数字组成！");
		//$('#registerAccount').focus();
		//return;
	}
	if( $("#registerPwd").val() === ""){
		alert( "登录密码不允许为空!" ); $("#registerPwd").focus();return;
	}
	if( $("#registerPwdCheck").val() !== $("#registerPwd").val()){
		alert( "两次密码不一致，请检查输入!" ); $("#registerPwdCheck").focus();return;
	}
	if($('#reg_password2').val()==''){
		alert('交易密码不能为空!');
		$('#reg_password2').focus();
		return false;
	}
	if($('#reg_conf_password2').val()==''){
		alert('确认交易密码不能为空!');
		$('#reg_conf_password2').focus();
		return false;
	}else{
		if($('#reg_password2').val()!=$('#reg_conf_password2').val()){
			alert('交易密码两次输入不一致!');
			$('#reg_conf_password2').focus();
			return false;
		}
	}
	if($('#reg_recommendUser').val()==''){
		alert('推荐会员不能为空!');
		$('#reg_recommendUser').focus();
		return false;
	}
	
	if($('#reg_userName').val()==''){
		alert('会员姓名不能为空!');
		$('#reg_userName').focus();
		return false;
	}
	
/*	if($('#reg_contactNo').val()==''){
		alert('接点人不能为空!');
		//$('#reg_contactNo').focus();
		return false;
	}
	
	if($('#reg_declarationCenters').val()==''){
		alert('运营中心不能为空!');
		$('#reg_declarationCenters').focus();
		return false;
	}*/
	
	if($('#reg_bankAccount').val()==''){
		alert('银行账号不能为空!');
		$('#reg_bankAccount').focus();
		return false;
	}
	
	if(($('#accountType').val()!='' && $('#accountType').val()!='财付通' && $('#accountType').val()!='支付宝') && $('#openBankAddress').val()==''){
		alert('银行开户地址不能为空');
		$('#openBankAddress').focus();
		return false;
	}
	
/*	if($('#reg_phone').val()==''){
		alert('联系电话不能为空!');
		$('#reg_phone').focus();
		return false;
	}
	if($('#reg_email').val()==''){
		alert('邮箱地址不能为空!');
		$('#reg_email').focus();
		return false;
	}
	
	if($('#reg_passwordAnswer').val()==''){
		alert('密保答案不允许为空!');
		$('#reg_passwordAnswer').focus();
		return false;
	}*/
	
	if( $("#registerMobile").val() === ""){
		alert( "手机号码不允许为空!" ); $("#registerMobile").focus();return;
	}
	if( !checkMobile($("#registerMobile").val())){
		alert( "手机号码不正确!" ); $("#registerMobile").focus();return;
	}
	
	if( $("#registerAuthCode").val() === ""){
		alert( "请输入手机收到的验证码!" ); $("#registerAuthCode").focus();return;
	}
	
	if(!$('#membershipAgreement').is(':checked')){
		alert( "请同意会员协议" ); return;
	}
	
	
	 var _frm = $('#register_form').serialize();
	 $.post('registerUser!saveRegisterUserAuth.action',_frm,function(res){
		 if(res=='success'){
			 $('#register_form').submit();
		 }else{
			 if(res=='loginNameExist')
				 alert('会员编号已存在!');
			 if(res=='recommendUserNotExist')
				 alert('推荐人编号不存在)!');
			 if(res=='RecommendUserNotActivation')
				 alert('推荐人未激活，不允许推荐注册!');
			 if(res=='contactNoNotExist')
				 alert('接点人编号不存在!');
			 if(res=='contactNoNotActivation')
				 alert('接点人未激活，不允许注册下一层会员!');
			 if(res=='declarationCenterUserNotExist')
				 alert('运营中心不存在!');
			 if(res=='ABCAreaFull')
				 alert('左右区已满!');
			 if(res=='aAreaExist')
				 alert('左区已注册!');
			 if(res=='bAreaExist')
				 alert('右区已注册!');
			 if(res=='aAreaNotExistNotAllowRegBArea')
				 alert('左区未注册，右区不允许注册!');
			 if(res=='cAreaExist')
				 alert('C区已注册!');
			 if(res=='abAreaNotExistNotAllowRegCArea')
				 alert('AB区未注册满，还不能注册C区!');
			 if(res=='cAreaOnlyAllowsOneselfReg')
				 alert('C区只能由自己注册!');
			 if(res=='MyNotRecommedLeftArea')
				 alert('先注册左区并开通才允许注册!');
			 if(res=='staticMoneyNotEnough')
				 alert('注册失败，子账户余额不足!');
		 }
	 });
	
}

/**
 * 检查账号是否存在
 * @param loginName
 * @param showId
 */
function checkLoginNameExist(loginName,showId){
	if(loginName!=''){
		$.post('registerUser!findUserNameByLoginName.action','registerUserDTO.loginName='+loginName,function(res){
			if(res!=null && res!=''){
				$('#'+showId).text("已注册");
			}else{
				$('#'+showId).text("可注册");
			}
			
		});	
	}
	
}


//change password1
function changePassword1(){
	if($('#cp1_forn').form('validate')){
		$.get('getCode.jsp'+'?random='+Math.random()*10+1,function(res){
			 res=res.replace(/(^\s*)|(\s*$)/g, ""); 
			 
			 if($('#cp_validCode').val()==res){
				 var _param = 'registerUserDTO.loginName='+$('#cp1_loginName').val()+'&pwdLevel='+$("input[@name='pwdLevel'][checked]").val();
				 $.post('registerUser!checkOrdPassword.action',_param+'&registerUserDTO.password1='+$('#cp1_oldPassword').val(),function(result){
					if(result){
						$.post('registerUser!updatePassword.action',_param+'&registerUserDTO.password1='+$('#cp1_newPassword').val(),function(result){
							alert('Successful modification(修改成功)!');
							window.location.reload();
						});
					}else{
						alert('The original password is not correct(原始密码不正确)!');
					}
				 });
			 }else{
				 alert('The code is incorrect(验证码不正确)!');
			 }
			 
			 
		})
		
		
		
	}
}

function checkLoginNameIsExist(id){
	var loginName=$('#'+id).val();
	$('#reg_recommendUser_isexist').text('Verifying(正在验证)......');
	if(loginName!=''){
		$.post('registerUser!loginNameExist.action','registerUserQueryDTO.loginName='+loginName,function(data){
			if(data)
				$('#reg_recommendUser_isexist').text('Game player number correct(玩家编号正确)!');
			else
				$('#reg_recommendUser_isexist').text('Game player number does not exist(玩家编号不存在)!');
		})
	}else{
		$('#reg_recommendUser_isexist').text('Game player number does not exist(玩家编号不存在)!');
	}
}

//修改个人信息
function changeBasicInfo(){
	if($('#basicInfo_div form').form('validate')){
		$.get('getCode.jsp'+'?random='+Math.random()*10+1,function(res){
			 res=res.replace(/(^\s*)|(\s*$)/g, ""); 
			 if($('#pi_validCode').val()==res){
				 var _frm = $('#updateInfo_form').serialize();
					$.post('registerUser!changeRegUserBasicInfo.action',_frm,function(data){
						alert('Successful modification(修改成功)!');
					})
			 }else{
				 alert('The code is incorrect(验证码不正确)!');
			 }
		});
		
	};
	
}
//交易密码验证
function twoLevelPwdValid(){
	if($('#two_level_pwd_form').form('validate')){
		$.get('getCode.jsp'+'?random='+Math.random()*10+1,function(res){
			 res=res.replace(/(^\s*)|(\s*$)/g, ""); 
			 if($('#login2_validCode').val()==res){
				 var _frm = $('#two_level_pwd_form').serialize();
					$.post('registerUser!userValidate.action',_frm,function(data){
						if(data!=null && data.loginName!=''){
							window.location='tm.jsp';
						}else{
							alert('The user name or password is not correct(用户名或密码不正确)!');
						}
						
					})
			 }else{
				 alert('The code is incorrect(验证码不正确)!');
			 }
		});
	}
	
	
}


function upPWQ(){
	if($('#basicInfo_div form').form('validate')){
		var _frm = $('#basicInfo_div form').serialize();
		$.post('registerUser!getBack.action',_frm,function(data){
			if(data=='userNonExist'){
				alert('The user does not exist(用户不存在)');
			}else if(data=='answerError'){
				alert('Wrong answer(答案错误)！');
			}else{
				alert('Find Password successfully(找回密码成功)！');
				window.location="index.jsp";
			}
			
		})
	}
}
function findPassWord(valid){
	if($('#loginName').val()=='' || $('#loginName').val()=='会员编号'){
		alert('Please enter your member number(请输入会员编号)!');
		return false;
	}
	if(valid=='yes'){
		var validCode = $('#userLogin_validCode').val();
		$.get('getCode.jsp'+'?random='+Math.random()*10+1,function(res){
			 res=res.replace(/(^\s*)|(\s*$)/g, ""); 
			 if(validCode==res){
				 var frm=$("#my-findPasswordform").serialize();
					$.post("registerUser!findUserPassword.action",frm,function(result){
						if(result=='noUser'){
							alert("Cannot find the membership number(无法找到该会员编号)");
						}else if(result=='emailSendError'){
							alert("Failed to send email, please contact the administrator.(邮件发送失败，请联系管理员)");
						}else{
							window.location="findPasswordSuccess.jsp?email="+result;
						}
					});
			}else{
				alert('The code is incorrect(验证码不正确)!');
			}
		});
	}
}
function findPDUpdate(){
	var password=$("#fp_password").val();
	var password1=$("#fp_password1").val();
	if(password!=''){
		if(password1!=password){
			alert("The two passwords do not match(两次密码输入不一致)！");
		}else{
			var frm=$("#my-updatePasswordform").serialize();
			$.post("registerUser!updateUserPassword.action",frm,function(data){
				if(data){
					alert("Successful in changing the password, please login(密码修改成功，请登录)！");
					window.location="login.jsp";
				}else{
					alert("Modify the password failed(密码修改失败)！");
				}
			});
		}
	}else{
		alert("The new password cannot be blank.(新密码不能为空)！");
	}
}


function findUserNameByLoginName(loginName,showId){
	$.post('registerUser!findUserNameByLoginName.action','registerUserDTO.loginName='+loginName,function(res){
		if(res!=''){
			$('#'+showId).text("* "+res);
		}else{
			$('#'+showId).text("* Does not exist(不存在)!");
		}
		
	});	
}
function useDefault(id){
	$('#'+id).val('0000');
}

function findcontact(inputid){
	$('#showContactNoList').html('');
	var height = 280;
	var width = 530;
	var url = 'DialogContact.jsp?loginName='+$('#'+inputid).val()+'&registerLocation='+$("input[name=registerUserDTO.registerLocation1][checked]").val();
	//alert(url);
	if(navigator.userAgent.indexOf("Chrome") >0 ){
		var iTop = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
		var iLeft = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
		var winOption =  "height="+height+",width="+width+",top="+iTop+",left="+iLeft+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,fullscreen=0";
		window.open(url,window, winOption);
	}else{
		var dlgResult = window.showModalDialog(url, window, 'dialogHeight: ' + height + 'px; dialogWidth: ' + width + 'px;edge: Raised; center: Yes; help: Yes;scroll:no; resizable: no; status: no;resizable:yes');
		if (dlgResult != null) { 
			//alert(dlgResult);
			var words = dlgResult.split(",");
			select(words[0],words[1]);
		}
	} 
	
	/*$.post('registerUser!findContactNo.action','loginName='+loginName+'&registerLocation='+$("input[name=registerUserDTO.registerLocation1][checked]").val(),function(data){
		if(data!=null){
			if(data.length==1){
				$('#reg_contactNo').val(data[0].loginName);
				$('#registerLocation').val(data[0].registerLocation);
				registerLocation
			}else{
				for(var i=0 ; data.length>i ; i++){
					$('#showContactNoList').append("登录名："+data[i].loginName+",姓名："+data[i].userName+",级别："+data[i].registerLevel+",<a href=javascript:select('"+data[i].loginName+"','"+data[i].registerLocation+"')>[选择]</a><br>");
				}
			}
		}else{
			alert("System can not find the contact person, please note about the selected area!.<br>系统未找到接点人，请注意所选择的左右区！");
		}
	});*/
}

function findcontact1(inputid){
	$('#showContactNoList').html('');
	$.post('registerUser!findContactNo.action','activeType=json&loginName='+$('#'+inputid).val()+'&registerLocation='+$("input[name=registerUserDTO.registerLocation1][checked]").val(),function(data){
	if(data!=null){
		if(data.length==1){
			select(data[0].loginName,data[0].registerLocation);
			//registerLocation
			alert('接点人获取成功');
		}else{
			for(var i=0 ; data.length>i ; i++){
				$('#showContactNoList').append("<br>登录名："+data[i].loginName+",姓名："+data[i].userName+",级别："+data[i].registerLevel+",<a href=javascript:select('"+data[i].loginName+"','"+data[i].registerLocation+"')>[选择]</a><br>");
			}
		}
	}else{
		alert("System can not find the contact person, please note about the selected area!.<br>系统未找到接点人，请注意所选择的左右区！");
	}
});
}

function select(loginName,registerLocation){
	$('#reg_contactNo').val(loginName);
	$('#registerLocation').val(registerLocation);
}

function userUpgrade(){
	if(confirm("升级前请确认是否有足够的余额!确定要升级吗?")){
		$('#userUpgrade_form').submit();
	}
}
function submitCheck(){
	
	return true;
}


function reTouZi(){
	if(confirm("复投前请确认是否有足够的余额!确定要复投吗?")){
		$('#reTouZi_form').submit();
	}
}

